package com.hzjiteng.newsbymvp.main.wedgit;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.hzjiteng.newsbymvp.R;
import com.hzjiteng.newsbymvp.about.widget.AboutFragment;
import com.hzjiteng.newsbymvp.images.widget.ImageFragment;
import com.hzjiteng.newsbymvp.main.presenter.MainPresenter;
import com.hzjiteng.newsbymvp.main.presenter.MainPresenterImpl;
import com.hzjiteng.newsbymvp.main.view.MainView;
import com.hzjiteng.newsbymvp.news.widget.NewsFragment;
import com.hzjiteng.newsbymvp.weather.widget.WeatherFragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity implements MainView {

    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle drawerToggle;
    private Toolbar toolbar;
    private MainPresenter mainPresenter;
    private NavigationView navigationView;

    private List<Fragment>fragmentList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI$Data();
    }

    private void initUI$Data() {
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
        drawerToggle.syncState();
        drawerLayout.setDrawerListener(drawerToggle);

        navigationView = (NavigationView) findViewById(R.id.navidation_view);
        setUpDrawContent(navigationView);

        mainPresenter = new MainPresenterImpl(this);

        initFragmentList();
        switchNews();
    }

    private void initFragmentList() {
        fragmentList = new ArrayList<>();
        Collections.addAll(fragmentList,new NewsFragment(),new ImageFragment(),new WeatherFragment(),new AboutFragment());
    }

    private void setUpDrawContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                item.setChecked(true);
                mainPresenter.menuSelected(item);
                drawerLayout.closeDrawers();
                return true;
            }
        });
    }

    @Override
    public void switchNews() {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content,fragmentList.get(0)).commit();
        toolbar.setTitle(R.string.news);
    }

    @Override
    public void switchIamges() {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content,fragmentList.get(1)).commit();
        toolbar.setTitle(R.string.images);
    }

    @Override
    public void switchWeather() {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content,fragmentList.get(2)).commit();
        toolbar.setTitle(R.string.weather);
    }

    @Override
    public void switchAbout() {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content,fragmentList.get(3)).commit();
        toolbar.setTitle(R.string.about);

    }
}
